package com.webfinal.backsystem.service.impl;

import com.webfinal.backsystem.dao.UserMapper;
import com.webfinal.backsystem.entity.User;
import com.webfinal.backsystem.request.user.*;
import com.webfinal.backsystem.response.user.DeleteResponse;
import com.webfinal.backsystem.response.user.RegisterResponse;
import com.webfinal.backsystem.response.user.UpdateInfoResponse;
import com.webfinal.backsystem.response.user.UserListResponse;
import com.webfinal.backsystem.service.ManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManagerServiceImpl implements ManagerService {
    private final static Logger LOG= LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;

    @Override
    public RegisterResponse addUser(AllUserRegisterRequest allUserRegisterRequest) {
        RegisterResponse response = new RegisterResponse();
        try{
            User user = new User();
            user.setUserId(0);
            user.setAccount(allUserRegisterRequest.getAccount());
            user.setPassword(allUserRegisterRequest.getPassword());
            user.setAuthority(allUserRegisterRequest.getAuthority());
            user.setUsername(allUserRegisterRequest.getUsername());
            user.setBirthday(allUserRegisterRequest.getBirthday());
            user.setSex(allUserRegisterRequest.getSex());
            if (userMapper.insert(user)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }
        }catch (Exception e){
            LOG.error("管理员操作：用户表插入数据失败");
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public DeleteResponse deleteUser(DeleteRequest deleteRequest) {
        DeleteResponse response = new DeleteResponse();
        try{
            User user = userMapper.selectByAccount(deleteRequest.getAccount());
            if(user == null){
                response.setResultCode("1");
                response.setResultMsg("未查到账户信息");
            }else {
                if(userMapper.deleteByaccount(deleteRequest.getAccount())==1){
                    response.setResultCode("0");
                    response.setResultMsg("delete success");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return response;
    }

    @Override
    public UserListResponse userList() {
        UserListResponse response = new UserListResponse();
        List<User> userList = userMapper.userlist();
        if(userList == null){
            response.setResultCode("1");
            response.setResultMsg("未查到用户信息");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setUserList(userList);
        }
        return  response;
    }

    @Override
    public UpdateInfoResponse updatePassword(UpdateInfoRequest updateInfoRequest) {
        UpdateInfoResponse response = new UpdateInfoResponse();
        User user = userMapper.selectByAccount(updateInfoRequest.getAccount());
        if(user == null){
            response.setResultCode("1");
            response.setResultMsg("未查到账户信息");
        }else{
            user.setPassword(updateInfoRequest.getNewPassword());
            if (userMapper.updateByPrimaryKey(user)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }else {
                response.setResultCode("1");
                response.setResultMsg("error");
            }
        }
        return response;
    }
}
